package main

import "fmt"

//时间复杂度为O(n^2)
func twoSum(nums []int, target int) []int {
	l := len(nums)
	for i := 0; i < l; i++ {
		for j := i + 1; j < l; j++ {
			if nums[i]+nums[j] == target {
				return []int{i, j}
			}
		}
	}
	return []int{}
}

//利用hash表时间复杂度为O(n),这里是直接将值存在了key里
func twoSum1(nums []int, target int) []int {
	hashTable := map[int]int{}
	for i, x := range nums {
		if p, ok := hashTable[target-x]; ok {
			return []int{p, i}
		}
		hashTable[x] = i
	}
	return nil
}

func main() {
	res := twoSum([]int{1, 2, 3}, 5)
	twoSum1([]int{1, 2, 3}, 5)
	fmt.Println(res)
}
